package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.InvoiceBuyMapper;
import com.ld.hpfs.service.InvoiceBuyService;

@Service
public class InvoiceBuyServiceImpl implements InvoiceBuyService{

	private static final Logger logger = LoggerFactory.getLogger(InvoiceBuyServiceImpl.class);
	
	@Autowired
	private InvoiceBuyMapper invoicebuymapper;
	
	// 获取发票购入一览信息
	@Override
	public ArrayList<HashMap<String, Object>> getInvoiceBuyList(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		int count = invoicebuymapper.getInvoiceBuyListCount(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return invoicebuymapper.getInvoiceBuyList(param);
		}
	}

	// 编辑保存
	@Override
	public int editInvoiceBuy(HashMap<String, Object> param) {
		invoicebuymapper.editInvoiceBuy(param);
		String invoiceBuyCode = param.get("invoiceBuyCode").toString();
		String companyId = param.get("companyId").toString();
		String userId = param.get("userId").toString();
		long invoiceBuySno = Long.parseLong(param.get("invoiceBuySno").toString());
		long invoiceBuyEno = Long.parseLong(param.get("invoiceBuyEno").toString());
		String invoiceBuyId = param.get("invoiceBuyId").toString();
		// 循环插入明细表
		for (long invoiceNo = invoiceBuySno; invoiceNo <= invoiceBuyEno; invoiceNo++) {
			HashMap<String, Object> paramfordetail = new HashMap<String, Object>();
			paramfordetail.put("invoiceNo", String.valueOf(invoiceNo));
			paramfordetail.put("invoiceBuyId", invoiceBuyId);
			paramfordetail.put("invoiceBuyCode", invoiceBuyCode);
			paramfordetail.put("companyId", companyId);
			paramfordetail.put("userId", userId);
			invoicebuymapper.saveInvoiceBuyDetail(paramfordetail);
			paramfordetail.remove("invoiceNo");
		}
		return 0;
	}
	// 新建保存
	@Override
	public int saveInvoiceBuy(HashMap<String, Object> param) {
		int maxId = invoicebuymapper.saveInvoiceBuy(param);
		String invoiceBuyCode = param.get("invoiceBuyCode").toString();
		String companyId = param.get("companyId").toString();
		String userId = param.get("userId").toString();
		String aaa = param.get("invoiceBuySno").toString();
		String bbb = param.get("invoiceBuyEno").toString();
		long invoiceBuySno = Long.parseLong(aaa);
		long invoiceBuyEno = Long.parseLong(bbb);
		// 循环插入明细表
		for (long invoiceNo = invoiceBuySno; invoiceNo <= invoiceBuyEno; invoiceNo++) {
			HashMap<String, Object> paramfordetail = new HashMap<String, Object>();
			paramfordetail.put("invoiceNo", String.valueOf(invoiceNo));
			paramfordetail.put("invoiceBuyId", String.valueOf(maxId));
			paramfordetail.put("invoiceBuyCode", invoiceBuyCode);
			paramfordetail.put("companyId", companyId);
			paramfordetail.put("userId", userId);
			invoicebuymapper.saveInvoiceBuyDetail(paramfordetail);
			paramfordetail.remove("invoiceNo");
		}
		return 0;
	}

	@Override
	public ArrayList<HashMap<String, Object>> getMachInfo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return invoicebuymapper.getMachInfo(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getMachNames(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return invoicebuymapper.getMachNames(param);
	}
	
	// 删除
	@Override
	public int deleteInvoiceList(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		invoicebuymapper.deleteInvoiceList(param);
		return 0;
	}

	@Override
	public ArrayList<HashMap<String, Object>> getInvoiceDataForEdit(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return invoicebuymapper.getInvoiceDataForEdit(param);
	}
}
